﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeOntogeny.X10Interface.States
{
	internal class SendReady<TNextState> : PortStateBase where TNextState: IPortState, new()
	{
		protected override void DoHandleNewBuffer(IRequestManager requestManager, byte[] newBuffer)
		{
			// Wait for the interface to respond with a ready acknowledgment
			if (newBuffer[0] == InterfaceReady)
			{
				Log.Info("Interface response: READY");
				requestManager.CurrentState = new TNextState();
			}
			else
			{
				requestManager.CurrentState = new ErrorState();
			}
		}

		/// <summary>
		/// Sends a command to the interface indicating the checksum was correct
		/// </summary>
		/// <param name="requestManager">
		/// Reference to the request manager
		/// </param>
		public override void HandleArrive(IRequestManager requestManager)
		{
			Log.Info("Sending checksum ACK to interface");

			// Ack complete. Send "OK for transmission" to the interface/devices.
			requestManager.Port.Write(new[] { OKForTransmission }, 0, 1);
		}

	}
}
